package cn.kgc.itrip.search.service.impl;

import cn.kgc.itrip.beans.common.ServerResponse;
import cn.kgc.itrip.beans.model.vo.SearchHotCityVO;
import cn.kgc.itrip.beans.model.vo.SearchHotelVO;
import cn.kgc.itrip.search.dao.BaseDao;
import cn.kgc.itrip.search.model.Hotel;
import cn.kgc.itrip.search.service.ISearchService;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author 阿泽
 */
@Service("iSearchService")
public class ISearchServiceimpl implements ISearchService {

    @Resource
    private BaseDao baseDao;

    /**
     * 根据热门城市查询酒店
     *
     * @param searchHotCityVO
     * @return
     */
    @Override
    public ServerResponse searchItripHotelListByHotCity(SearchHotCityVO searchHotCityVO) {
        //默认走全查
        SolrQuery solrQuery = new SolrQuery("*:*");
        if (searchHotCityVO.getCityId() != null) {
            solrQuery.addFilterQuery("cityId:" + searchHotCityVO.getCityId());
        }
        try {
            return baseDao.queryData(solrQuery, null, null, Hotel.class);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
        return ServerResponse.createByError();
    }

    /**
     * 查询酒店分页
     *
     * @param searchHotelVO
     * @return
     */

    @Override
    public ServerResponse searchItripHotelPage(SearchHotelVO searchHotelVO) {
        if (searchHotelVO.getPageNo() == null || searchHotelVO.getPageNo() == 0) {
            searchHotelVO.setPageNo(1);
        }
        if (searchHotelVO.getPageSize() == null || searchHotelVO.getPageSize() == 0) {
            searchHotelVO.setPageSize(5);
        }
        //开始组装查询
        SolrQuery solrQuery = new SolrQuery("*:*");
        StringBuffer sb=new StringBuffer();
        if (StringUtils.isNotBlank(searchHotelVO.getDestination())) {
            sb.append("destination:" + searchHotelVO.getDestination());
        } else {
            sb.append("destination:北京");
        }
        //在目的地后面可查询关键字
        if (StringUtils.isNotBlank(searchHotelVO.getKeywords())) {
            sb.append(" AND keyword :" + searchHotelVO.getKeywords());
        }
        solrQuery.addFilterQuery(sb.toString());
        //查询酒店级别
        if (StringUtils.isNotBlank(String.valueOf(searchHotelVO.getHotelLevel()))&& searchHotelVO.getHotelLevel()!=null) {
            sb.append(" AND hotelLevel :" + searchHotelVO.getHotelLevel());
        }
        solrQuery.addFilterQuery(sb.toString());
        //tradeAreaIds 商圈id
        if ( StringUtils.isNotBlank(searchHotelVO.getTradeAreaIds())) {
            String[] split = StringUtils.split(searchHotelVO.getTradeAreaIds(), ",");
            StringBuffer sb1=new StringBuffer();
            for (int i = 0; i <split.length ; i++) {
                if (i==0){
                    sb.append("tradingAreaIds : * ,"+split[i]+" , *");
                }else {
                    sb.append(" OR tradingAreaIds : *,"+split[i]+" , *");
                }
            }
            solrQuery.addFilterQuery(sb1.toString());
        }
        //minPrice 最低价
        if (searchHotelVO.getMinPrice() != null && searchHotelVO.getMinPrice() == null) {
            sb.append(" AND minPrice:[ " + searchHotelVO.getMinPrice() + " TO * ]");
        }
        solrQuery.addFilterQuery(sb.toString());
        //maxPrice 最高价
        if (searchHotelVO.getMaxPrice() != null && searchHotelVO.getMinPrice() == null) {
            sb.append(" AND minPrice:[* TO " + searchHotelVO.getMaxPrice() + "]");
        }
        solrQuery.addFilterQuery(sb.toString());
        if (searchHotelVO.getMinPrice() != null && searchHotelVO.getMaxPrice() != null) {
            solrQuery.addFilterQuery(" AND minPrice:[ " + searchHotelVO.getMinPrice() +
                    " TO " + searchHotelVO.getMaxPrice() + "]");
        }
        //featureIds 酒店特色id
        if (searchHotelVO.getFeatureIds() != null) {
            String[] split = searchHotelVO.getFeatureIds().split(",");
            StringBuffer sb2 = new StringBuffer();
            for (int i = 0; i < split.length; i++) {
                if (i == 0) {
                    sb2.append(" featureIds: *, " + split[i] + ",*");
                } else {
                    sb2.append(" OR featureIds: *," + split[i] + ",*");
                }
            }
            solrQuery.addFilterQuery(sb2.toString());
        }
        //按照某个字段升序ascSort

        if (searchHotelVO.getAscSort() != null) {
            solrQuery.setSort(searchHotelVO.getAscSort(), SolrQuery.ORDER.asc);
        }
        //按照某个字段降序
        if (searchHotelVO.getDescSort() != null) {
            solrQuery.setSort(searchHotelVO.getDescSort(), SolrQuery.ORDER.asc);
        }
       /* //入住日期CheckInDate
        if (searchHotelVO.getCheckInDate() != null) {
            solrQuery.addFilterQuery(" AND checkInDate:" + searchHotelVO.getCheckInDate());
        }
        //退房日期checkOutDate
        if (searchHotelVO.getCheckOutDate() != null) {
            solrQuery.addFilterQuery(" AND checkOutDate:" + searchHotelVO.getCheckOutDate());
        }*/

        try {
            return baseDao.queryData(solrQuery, searchHotelVO.getPageNo(), searchHotelVO.getPageSize(), Hotel.class);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
        return ServerResponse.createByError();
    }
}
